package com.example.gokuplayalong;

import static com.example.gokuplayalong.bases.exception.ExceptionUtils.getTraceInfo;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.example.gokuplayalong.bases.beans.AppParams;
import com.example.gokuplayalong.bases.exception.ExceptionUtils;
import com.example.gokuplayalong.bases.mobsdk.MobSdk;
import com.example.gokuplayalong.bases.permission.PermissionUtils;
import com.example.gokuplayalong.service.fragmentsmanage.SysManagerFragments;
import com.kongzue.dialogx.DialogX;
import com.kongzue.dialogx.dialogs.MessageDialog;
import com.kongzue.dialogx.interfaces.OnBindView;
import com.kongzue.dialogx.interfaces.OnDialogButtonClickListener;
import com.kongzue.dialogx.util.TextInfo;

import java.io.File;
import java.util.List;

import me.jessyan.autosize.AutoSizeCompat;
import pub.devrel.easypermissions.EasyPermissions;

public class MainActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    public static MainActivity INSTANCE = null;

    public Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initApp();
    }


    private void initApp() {
        try {

            DialogX.init(this);
            INSTANCE = this;
//            ActionBar actionBar = getSupportActionBar();
//            if (actionBar != null) {
//                actionBar.hide();
//                actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FF0000")));
//
//            }

            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

            AppParams.getInstance().mediaDir = getApplication().getFilesDir().getAbsolutePath();

            File file = new File(AppParams.getInstance().mediaDir, "init");

            MessageDialog userRequireDialog = new MessageDialog("用户须知", "", "同意并继续", "不同意").setCustomView(new OnBindView<MessageDialog>(R.layout.fragment_user_setting_dialog_protocol_policy_agree) {
                @Override
                public void onBind(MessageDialog dialog, View v) {


                    v.findViewById(R.id.dialog_protocol_policy_agree_protocol).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                Uri uri = Uri.parse(AppParams.getInstance().protocolUrl);
                                intent.setData(uri);
                                startActivity(intent);
                            } catch (Throwable e) {
                                ExceptionUtils.getException(getTraceInfo(), e);
                            }
                        }
                    });
                    v.findViewById(R.id.dialog_protocol_policy_agree_policy).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            try {
                                Intent intent = new Intent(Intent.ACTION_VIEW);
                                Uri uri = Uri.parse(AppParams.getInstance().policyUrl);
                                intent.setData(uri);
                                startActivity(intent);
                            } catch (Throwable e) {
                                ExceptionUtils.getException(getTraceInfo(), e);
                            }
                        }
                    });

                }
            });
            userRequireDialog.setCancelable(false);
            userRequireDialog.setCancelTextInfo(new TextInfo().setFontColor(getResources().getColor(R.color.red)));
            userRequireDialog.setOkButtonClickListener(new OnDialogButtonClickListener<MessageDialog>() {
                @Override
                public boolean onClick(MessageDialog dialog, View v) {
                    try {
                        initHasAgree();
                        File file = new File(AppParams.getInstance().mediaDir, "init");
                        if (!file.exists()) file.createNewFile();


                    } catch (Throwable e) {
                        ExceptionUtils.getException(getTraceInfo(), e);
                    }
                    return false;
                }
            });


            userRequireDialog.setCancelButtonClickListener(new OnDialogButtonClickListener<MessageDialog>() {
                @Override
                public boolean onClick(MessageDialog dialog, View v) {
                    System.exit(0);
                    return false;
                }
            });

            if (!file.exists()) {
                userRequireDialog.show();
            }
            else {
                initHasAgree();
            }


        } catch (Throwable e) {
            ExceptionUtils.getException(getTraceInfo(), e);
        }

    }

    private void initHasAgree() {

        try {

            // HwAds.init(this);
            MobSdk.init();

            // ByteDanceSDK.getInstance().init(this);
            SysManagerFragments.init(getSupportFragmentManager());

            AppParams.getInstance().appRunParams.getJsonObject().clear();
            AppParams.getInstance().appRunParams.getJsonObject().put("chatNum", 1);
            AppParams.getInstance().appRunParams.getJsonObject().put("chatCount", 20);


        } catch (Throwable e) {
            ExceptionUtils.getException(getTraceInfo(), e);
        }


    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return SysManagerFragments.onKeyDown(keyCode, event);
    }


    public static MainActivity getInstance() {
        return INSTANCE;
    }


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int i, @NonNull List<String> list) {
        PermissionUtils.permissionUtilsInterface.onPermissionsGranted(i, list);
    }

    @Override
    public void onPermissionsDenied(int i, @NonNull List<String> list) {
        PermissionUtils.permissionUtilsInterface.onPermissionsDenied(i, list);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MobSdk.destroy();
    }

    @Override
    public Resources getResources() {
        if (Looper.myLooper() == Looper.getMainLooper()) {
            AutoSizeCompat.autoConvertDensityOfGlobal((super.getResources()));//如果没有自定义需求用这个方法
        }
        return super.getResources();
    }


    @Override
    protected void onStart() {
        super.onStart();
        initStart();
    }

    private void initStart() {
        try {

        } catch (Throwable e) {
            ExceptionUtils.getException(getTraceInfo(), e);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();


    }


    @Override
    protected void onPause() {
        super.onPause();
    }
}